package com.group.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.group.dao.GeneralBeanDao;
import com.group.model.User;
import com.group.model.UserTicker;
import com.group.model.UserTickerLog;

/**
 * 币service
 * 
 * @author zhaolei
 *
 */
@Service
public class TickerService {
	@Autowired
	GeneralBeanDao beanDao;

	public UserTickerLog addTicker(Long userId, String cid, Double num, String reason) {
		return addTicker(userId, cid, num, reason, null);
	}

	public UserTickerLog addTickerCatch(Long userId, String cid, Double num, String reason) {
		return addTickerCatch(userId, cid, num, reason, null);
	}

	public UserTickerLog addTicker(Long userId, String cid, Double num, String reason, Long otherUserId) {

		return addTicker(userId, cid, num, reason, otherUserId, "已完成", null, null, null);
	}

	public UserTickerLog addTickerCatch(Long userId, String cid, Double num, String reason, Long otherUserId) {

		return addTickerCatch(userId, cid, num, reason, otherUserId, "已完成", null, null, null);
	}


	/**
	 * 增加用户钱包
	 * 
	 * @param userId
	 * @param cid
	 * @param num
	 * @param reason
	 *            原因
	 */
	public UserTickerLog addTicker(Long userId, String cid, Double num, String reason, Long otherUserId, String state, String billNo, Double fee, String address) {
		UserTicker ut = new UserTicker();
		ut.setCid(cid);
		ut.setUserId(userId);

		Double cashing = null;
		if (reason.contains("提现") && !reason.contains("竞猜") && !reason.contains("失败"))
			cashing = num;

		UserTicker exist = beanDao.queryOne(ut);
		if (exist != null) {
			exist.setNum(exist.getNum() + num);
			exist.setCashing(cashing);
			beanDao.save(exist);

		} else {
			ut.setNum(num);
			ut.setCashing(cashing);
			beanDao.save(ut);
		}

		// 添加日志
		if (state == null)
			state = "已完成";
		UserTickerLog log = new UserTickerLog();
		log.setCid(cid);
		log.setNum(num);
		// 设置手续费
		if (fee == null)
			log.setFee(fee);

		log.setReason(reason);
		log.setUserId(userId);
		try {
			User user = beanDao.quickQueryOneById(User.class, userId);
			log.setUserName(user.getName());
			log.setUserTel(user.getTel());
		} catch (Exception e) {
		}
		log.setOtherUserId(otherUserId);
		log.setState(state);
		log.setBillNo(billNo);
		log.setAddress(address);
		beanDao.save(log);

		return log;
	}

	/**
	 * 数据计算分红
	 *
	 * @param userId
	 * @param cid
	 * @param num
	 * @param reason
	 *            原因
	 */
	public UserTickerLog addTickerCatch(Long userId, String cid, Double num, String reason, Long otherUserId, String state, String billNo, Double fee, String address) {
		/*UserTicker ut = new UserTicker();
		ut.setCid(cid);
		ut.setUserId(userId);

		Double cashing = null;
		if (reason.contains("提现") && !reason.contains("竞猜") && !reason.contains("失败"))
			cashing = num;

		UserTicker exist = beanDao.queryOne(ut);
		if (exist != null) {
			exist.setNum(exist.getNum() + num);
			exist.setCashing(cashing);
			beanDao.save(exist);

		} else {
			ut.setNum(num);
			ut.setCashing(cashing);
			beanDao.save(ut);
		}*/

		// 添加日志
		if (state == null)
			state = "已完成";
		UserTickerLog log = new UserTickerLog();
		log.setCid(cid);
		log.setNum(num);
		// 设置手续费
		if (fee == null)
			log.setFee(fee);

		log.setReason(reason);
		log.setUserId(userId);
		try {
			User user = beanDao.quickQueryOneById(User.class, userId);
			log.setUserName(user.getName());
			log.setUserTel(user.getTel());
		} catch (Exception e) {
		}
		log.setOtherUserId(otherUserId);
		log.setState(state);
		log.setBillNo(billNo);
		log.setAddress(address);
		beanDao.save(log);

		return log;
	}
	/**
	 * 获取用户指定币的钱包
	 * 
	 * @param userId
	 * @param cid
	 */
	public UserTicker getUserTicker(Long userId, String cid) {
		UserTicker ut = new UserTicker();
		ut.setCid(cid);
		ut.setUserId(userId);

		UserTicker exist = beanDao.queryOne(ut);
		if (exist == null) {
			ut.setNum(0d);
			beanDao.save(ut);
			return ut;
		}
		return exist;
	}
}
